package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.data.R;
import com.dk.util.PageResult;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//模板管理
@Controller
@RequestMapping("specif")
public class SpecificationController {


    @DubboReference
    SpecificationService sfs;

    //查询全部页面跳转
    @RequestMapping("tiao")
    public String tiao(){
        return "admin/specification";
    }


    //分页查询全部
    @RequestMapping("getall")
    @ResponseBody
    public Map<String, Object> getall(@RequestParam(defaultValue = "1") Integer pagenum, @RequestParam(defaultValue = "4") Integer pagesize){
        PageResult p = sfs.getall(pagenum, pagesize);
        Map<String, Object> m=new HashMap<>();
        m.put("rows",p.getRows());
        m.put("total",p.getTotal());
        return  m;
    }


    //增加跳转
    @RequestMapping("addtiao")
    public String addtiao(){
        return "specification/addspec";
    }


    //增加数据
    @RequestMapping("addspecif")
    @ResponseBody
    //接收后台传过来的数据,格式是”[{},{},{}]“
    //将传过来的json格式需要转换为list类型
    public R addspecif(TbSpecification tb, String specOptionJson){
        //将json转换为list类型，（json数据，要转换成的泛型）
        List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(specOptionJson, TbSpecificationOption.class);
        sfs.addspecifs(tb,tbSpecificationOptions);
        return R.ok();
    }


    //修改回显
    @RequestMapping("updatetiao")
    @ResponseBody
    public R updatetiao(Long id){
       SpecVo vo= sfs.findSpecbyid(id);

        return R.ok().setData(vo);
    }

    //修改
    @RequestMapping("updatespec")
    @ResponseBody
    public R updatespec(TbSpecification tb, String specOptionJson){
        //将json转换为list类型，（json数据，要转换成的泛型）
        List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(specOptionJson, TbSpecificationOption.class);
        sfs.updatespec(tb,tbSpecificationOptions);
        return R.ok();
    }


    //删除
    @RequestMapping("deleteid")
    @ResponseBody
    public R deleteid(Long id){
        sfs.deleteid(id);
        return R.ok();
    }



}
